package edu.whu.rose.clock.ssql.binding;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

import edu.whu.rose.clock.ssql.graph.RRadiusSubGraph;

public class SubGraphBinding extends TupleBinding {

	public Object entryToObject(TupleInput input) {
		int[] vertexes = new int[input.readInt()];
		for (int i = 0; i < vertexes.length; i++) {
			vertexes[i] = input.readInt();
		}
		int[] startVertexes = null;
		int[] endVertexes = null;
		if (input.readBoolean()) {
			int size = input.readInt();
			startVertexes = new int[size];
			endVertexes = new int[size];
			for (int i = 0; i < size; i++) {
				startVertexes[i] = input.readInt();
				endVertexes[i] = input.readInt();
			}
		}
//		Triple[] edges = new Triple[input.readInt()];
//		for (int i = 0; i < edges.length; i++) {
//			int startVertex = input.readInt();
//			int endVertex = input.readInt();
//			String[] relationships = new String[input.readInt()];
//			for (int j = 0; j < relationships.length; j++) {
//				relationships[j] = input.readString();
//			}
//			edges[i] = new Triple(startVertex, endVertex, relationships);
//		}
		return new RRadiusSubGraph(vertexes, startVertexes, endVertexes);
	}

	public void objectToEntry(Object object, TupleOutput output) {
		RRadiusSubGraph rrsg = (RRadiusSubGraph)object;
		int[] vertexes = rrsg.getVertexes();
		output.writeInt(vertexes.length);
		for (int i = 0; i < vertexes.length; i++) {
			output.writeInt(vertexes[i]);
		}
		if (rrsg.getStartVertexes() == null || rrsg.getEndVertexes() == null) {
			output.writeBoolean(false);
		}
		else {
			output.writeBoolean(true);
			output.writeInt(rrsg.getStartVertexes().length);
			for (int i = 0; i < rrsg.getStartVertexes().length; i++) {
				output.writeInt(rrsg.getStartVertexes()[i]);
				output.writeInt(rrsg.getEndVertexes()[i]);
			}
		}
//		Triple[] edges = rrsg.getEdges();
//		if (edges != null) {
//			output.writeInt(edges.length);
//			for (int i = 0; i < edges.length; i++) {
//				output.writeInt(edges[i].getStartVertex());
//				output.writeInt(edges[i].getEndVertex());
//				String[] relationships = edges[i].getRelationships();
//				output.writeInt(relationships.length);
//				for (int j = 0; j < relationships.length; j++) {
//					output.writeString(relationships[j]);
//				}
//			}
//		}
	}

}
